package com.xuecheng.content.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.attribute.standard.Media;
import java.util.Date;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-09-14
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private TeachplanService teachplanService;
    @Autowired
    private MediaClient mediaClient;
    @Autowired
    private CourseBaseService courseBaseService;
    @Override
    public ResponseResult associationMediaWithTeachMedia(BindTeachplanMediaDto dto) {
        //生成媒资-课程绑定表
        //1.参数检验
        if ((ObjectUtil.isEmpty(dto.getMediaId())||ObjectUtil.isEmpty(dto.getTeachplanId()))) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //判断状态
        //2-1.查询出课程计划
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        //2-2.查询课程信息
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        //2-3.判断课程状态
        if ((courseBase.getAuditStatus().equals(CourseConstants.PUBLISH_STATUS)
        ||courseBase.getAuditStatus().equals(CourseConstants.AUDIT_COMMIT_STATUS)
        ||courseBase.getAuditStatus().equals(CourseConstants.AUDIT_PASTED_STATUS))) {
            throw new BusinessException(ErrorCode.MEDIABINDERROR);
        }
        //3.通过feign调用媒资服务
        //3-1.调用
        ResponseResult<MediaVo> responseResult = mediaClient.associationMediaWithTeachPlan(dto.getMediaId());
        //3-1.判断调用是否成功
        if ((responseResult.getCode()!=0)) {
            throw new BusinessException(ErrorCode.DATAERROR);
        }

        //封装媒资计划对象
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(dto.getMediaId());
        teachplanMedia.setTeachplanId(dto.getTeachplanId());
        teachplanMedia.setCourseId(courseBase.getId());
        teachplanMedia.setMediaFilename(responseResult.getData().getFilename());
        teachplanMedia.setChangePeople(AuthInfoHolder.getUserId().toString());
        teachplanMedia.setCompanyId(AuthInfoHolder.getCompanyId());
        teachplanMedia.setCreateDate(new Date());
        //保存到数据库
        this.save(teachplanMedia);
        //返回ok
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTeachMedia(Integer teachPlanMediaId) {
        //参数校验
        if ((ObjectUtil.isEmpty(teachPlanMediaId))) {
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        //1.查询TeachplanMedia对象
        TeachplanMedia teachplanMedia = this.getById(teachPlanMediaId);
        //2.判断课程状态
        //2-1.查询出课程对象
        Long courseId = teachplanMedia.getCourseId();
        CourseBase courseBase = courseBaseService.getById(courseId);
        //2-2.判断状态
        if ((StrUtil.equals(courseBase.getAuditStatus(), CourseConstants.AUDIT_COMMIT_STATUS)
        ||StrUtil.equals(courseBase.getAuditStatus(),CourseConstants.AUDIT_PASTED_STATUS)
        ||StrUtil.equals(courseBase.getAuditStatus(),CourseConstants.PUBLISH_STATUS))) {
            throw new BusinessException(ErrorCode.DELETEERROR);
        }
        //3.删除媒资
        this.removeById(teachplanMedia);


        return ResponseResult.okResult();
    }
}
